package com.heiyu.api.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.heiyu.api.request.AdvicePutRequest;
import com.heiyu.api.request.UserUpdateRequest;
import com.heiyu.api.response.CommonResponse;
import com.heiyu.api.response.MoneyTranResponse;
import com.heiyu.api.response.UserInfoResponse;
import com.heiyu.api.response.WechatResponse;
import com.heiyu.api.response.model.CommonModel;
import com.heiyu.api.response.model.MoneyTranModel;
import com.heiyu.api.response.model.UserInfoModel;
import com.heiyu.api.response.model.WechatInfoModel;
import com.heiyu.constants.ResponseMsgEnum;
import com.heiyu.controller.ApiBaseController;
import com.heiyu.dao.UserAdviceMapper;
import com.heiyu.dao.UserConfigMapper;
import com.heiyu.dao.UserInfoMapper;
import com.heiyu.dao.UserVirtualAssetsInfoMapper;
import com.heiyu.entity.UserAdvice;
import com.heiyu.entity.UserConfig;
import com.heiyu.entity.UserFollow;
import com.heiyu.entity.UserInfo;
import com.heiyu.utils.MoneyUtils;
import com.heiyu.utils.RangeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by mountain on 18/6/11.
 */
@Api
@RestController
@RequestMapping("api/v1/user")
public class UserController extends ApiBaseController {

    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private UserConfigMapper userConfigMapper;
    @Autowired
    private UserVirtualAssetsInfoMapper userVirtualAssetsInfoMapper;
    @Autowired
    private UserAdviceMapper userAdviceMapper;

    @ApiOperation(value = "get",notes = "获取个人信息")
    @RequestMapping(value = "get",method = RequestMethod.GET)
    public UserInfoResponse get(){
        UserInfoResponse response = new UserInfoResponse();
        UserInfo userInfo = getUser();
        if(userInfo == null){
            response.error(ResponseMsgEnum.USER_NOT_LOGIN);
            return response;
        }
        userInfo = this.userInfoMapper.selectPrimary(userInfo.getId());
        UserConfig userConfig = this.userConfigMapper.selectPrimary(userInfo.getId());
        UserInfoModel model = new UserInfoModel();
        model.setAvator(userInfo.getAvator());
        model.setNickName(StringUtils.isEmpty(userInfo.getNickName())?userInfo.getTelphone():userInfo.getNickName());
        model.setBaseCoinType(userConfig.getDefaultMoneyType());
        model.setColorConfig(userConfig.getColorConfig());
        model.setMarketMessageConfig(userConfig.getMessageConfig());
        Map<String,Double> map = userVirtualAssetsInfoMapper.persionalTotal(userInfo.getId());
        if(defaultSymbol()){
            model.setValueAmount(MoneyUtils.formatter(new BigDecimal(map.get("nowTotalPrice")),true));
            model.setDiffPrice(MoneyUtils.formatter(new BigDecimal(map.get("diffPrice")),true));
        }else{
            String str = redisUtils.hget("COIN_GLOBAL","CNY");
            JSONObject obj = JSON.parseObject(str);
            model.setValueAmount(MoneyUtils.formatter(new BigDecimal(map.get("nowTotalPrice")).multiply(new BigDecimal(obj.getDouble("price_usd"))),true));
            model.setDiffPrice(MoneyUtils.formatter(new BigDecimal(map.get("diffPrice")).multiply(new BigDecimal(obj.getDouble("price_usd"))),true));
        }
        model.setRange(RangeUtil.formatter(new BigDecimal(map.get("percent"))));
        response.setData(model);
        response.success();
        return response;
    }

    @ApiOperation(value = "update",notes = "修改个人信息")
    @RequestMapping(value = "update",method = RequestMethod.POST)
    public CommonResponse update(@RequestBody UserUpdateRequest request){
            CommonResponse response = new CommonResponse();
            UserInfo userInfo = getUser();
            if(userInfo == null){
                response.error(ResponseMsgEnum.USER_NOT_LOGIN);
            }else{
                UserInfo updateUser = new UserInfo();
                updateUser.setId(userInfo.getId());
                updateUser.setAvator(request.getAvator());
                updateUser.setNickName(request.getNickName());
                this.userInfoMapper.updateById(updateUser);
                UserConfig userConfig = this.userConfigMapper.selectPrimary(userInfo.getId());
                if(userConfig == null){
                    userConfig = new UserConfig();
                    userConfig.setUserId(userInfo.getId());
                    userConfig.setId(userInfo.getId());
                    userConfig.setMessageConfig(request.getMarketMessageConfig());
                    userConfig.setDefaultMoneyType(request.getBaseCoinType());
                    userConfig.setColorConfig(request.getColorConfig());
                    this.userConfigMapper.insert(userConfig);
                }else{
                    userConfig.setMessageConfig(request.getMarketMessageConfig());
                    userConfig.setDefaultMoneyType(request.getBaseCoinType());
                    userConfig.setColorConfig(request.getColorConfig());
                    this.userConfigMapper.updateById(userConfig);
                }
                response.success();
            }
            return response;
    }


    @ApiOperation(value = "advice/put",notes = "意见反馈")
    @RequestMapping(value = "advice/put",method = RequestMethod.POST)
    public CommonResponse advicePut(@RequestBody AdvicePutRequest request){
            CommonResponse response  = new CommonResponse();
            UserInfo userInfo = getUser();
            if(userInfo == null){
                response.error(ResponseMsgEnum.USER_NOT_LOGIN);
            }else{
                UserAdvice userAdvice = new UserAdvice();
                userAdvice.setContent(request.getContent());
                userAdvice.setUserId(userInfo.getId());
                userAdvice.setUserName(userInfo.getTelphone());
                this.userAdviceMapper.insert(userAdvice);
                response.success();
            }
            return response;
    }

    @ApiOperation(value = "wechat",notes = "微信")
    @RequestMapping(value = "wechat",method = RequestMethod.GET)
    public WechatResponse wechat(){
        WechatResponse response = new WechatResponse();
        response.setData(new WechatInfoModel());
        response.success();
        response.getData().setImgUrl("https://dafenqi.oss-cn-hangzhou.aliyuncs.com/1530692346955113.png");
        response.getData().setWecahtId("LMKF0246");
        return response;
    }

    @ApiOperation(value = "money/tran/get",notes = "汇率信息")
    @ApiImplicitParam(name = "target",value = "汇率主键非比穿，CNY／USD")
    @RequestMapping(value = "money/tran/get",method = RequestMethod.GET)
    public MoneyTranResponse moneyTran(@RequestParam(name = "target",required = false)String target){
        MoneyTranResponse response = new MoneyTranResponse();
        response.setData(new MoneyTranModel());
        if(target== null || target.length()==0){
            target="CNY";
        }
        String str = redisUtils.hget("COIN_GLOBAL",target);
        if(!StringUtils.isEmpty(str)){
            response.getData().setData(JSON.parseObject(str,HashMap.class));
        }
        response.success();
        return response;
    }
}
